﻿using EntityAop.Interceptors.InterfacesToImplement;
using EntityAop.Interceptors.Support;

namespace EntityAop.Interceptors
{
    public class EqualsInterceptor : BaseInterceptor
    {
        protected override void PerformProceed(Castle.DynamicProxy.IInvocation invocation)
        {
            var argument = invocation.Arguments[0];

            var argumentAsProxy = argument as ITargetAccessor;

            bool result = false;

            if (argumentAsProxy != null)
            {
                result = InvocationHelper.GetInvocationTarget(invocation).Equals(argumentAsProxy.GetTarget());
            }
            else if (argument != null)
            {
                result = InvocationHelper.GetInvocationTarget(invocation).GetHashCode() == argument.GetHashCode();
            }

            invocation.ReturnValue = result;
        }
    }
}
